import { _decorator, AudioClip, Component, director, EventTarget, Node } from 'cc';
import { BombUI } from './UI/BombUI';
import { ScoreUI } from './UI/ScoreUI';
import { Player } from './Player';
import { GameOverUI } from './UI/GameOverUI';
import { AudioMgr } from './AudioMgr';
import { GameStage } from './GameStage';
const { ccclass, property } = _decorator;

const eventTarget = new EventTarget();

@ccclass('GameManager')
export class GameManager extends Component {

    private static instance:GameManager;

    gameStage:GameStage = new GameStage();

    @property
    private bombNumber:number = 0;
    @property(BombUI)
    bombUI:BombUI = null;

    @property
    private score:number = 0;
    @property(ScoreUI)
    scoreUI:ScoreUI = null;

    @property(Node)
    pauseButton:Node = null;
    @property(Node)
    resumeButton:Node = null;

    @property(Player)
    player:Player = null;

    @property(GameOverUI)
    gameOverUI:GameOverUI = null;

    @property(AudioClip)
    gameMusic:AudioClip = null;

    @property(AudioClip)
    buttonAudio:AudioClip = null;

    @property(AudioClip)
    gameOverAudio:AudioClip = null;

    public static getInstance():GameManager {
        return this.instance;
    }

    protected onLoad(): void {
        GameManager.instance = this;

        eventTarget.on("onScoreChange", this.scoreUI.onScoreChange, this.scoreUI);
        eventTarget.on("onBombChange", this.bombUI.onBombChange, this.bombUI);
    }

    protected start(): void {
        AudioMgr.inst.play(this.gameMusic, 0.4, true);
    }

    protected onDestroy(): void {
        eventTarget.off("onBombChange", this.bombUI.onBombChange, this.bombUI);
        eventTarget.off("onScoreChange", this.scoreUI.onScoreChange, this.scoreUI);
    }

    public getGameStage():number {
        return this.gameStage.stage;
    }

    public getStage2Switch():boolean {
        return this.gameStage.stage2Switch;
    }

    public setStage2Switch(stageSwitch:boolean) {
        this.gameStage.stage2Switch = stageSwitch;
    }

    public addBomb() {
        this.bombNumber += 1;
        eventTarget.emit("onBombChange", this.bombNumber);
    }
    public useBomb() {
        this.bombNumber -= 1;
        eventTarget.emit("onBombChange", this.bombNumber);
    }

    public getBombNumber() {
        return this.bombNumber;
    }

    public addScore(score:number) {
        this.score += score;
        this.gameStage.computeStage(this.score);
        eventTarget.emit("onScoreChange", this.score);
    }

    gameOver() {
        AudioMgr.inst.playOneShot(this.gameOverAudio, 0.5);
        this.onPauseButtonClick();
        let hScore = localStorage.getItem("HighestScore");

        let hScoreInt = 0;
        if(hScore) {
            hScoreInt = parseInt(hScore, 10);
        } else {
            hScoreInt = this.score;
        }

        if(this.score >= hScoreInt) {
            hScoreInt = this.score;
            localStorage.setItem("HighestScore", this.score.toString());
        }
        this.gameOverUI.showGameOverUI(hScoreInt, this.score);
    }

    onPauseButtonClick() {
        AudioMgr.inst.playOneShot(this.buttonAudio, 0.5);
        AudioMgr.inst.pause();
        director.pause();
        this.player.disableControl();
        this.pauseButton.active = false;
        this.resumeButton.active = true;
    }

    onResumeButtonClick() {
        AudioMgr.inst.playOneShot(this.buttonAudio, 0.5);
        AudioMgr.inst.resume();
        director.resume();
        this.player.enableControl();
        this.pauseButton.active = true;
        this.resumeButton.active = false;
    }

    restartButtonClick() {
        AudioMgr.inst.playOneShot(this.buttonAudio, 0.5);
        director.loadScene(director.getScene().name);
        this.onResumeButtonClick();
    }

    quitButtonClick() {

    }

    isHaveBomb() {
        return this.bombNumber > 0;
    }

}

